package com.qf.service;

import com.netflix.discovery.converters.Auto;
import com.qf.constant.CacheConstants;
import com.qf.constant.OperatorConstants;
import com.qf.feign.CacheFeign;
import com.qf.model.StandardSubmit;
import com.qf.util.CheckMobileOperator;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Map;

/**
 * author:z
 * time:2021/12/29 17:24
 * description:号段补全策略
 */
@Service(value = "phase")
@Slf4j
public class PhaseChainManagerImpl implements FilterChainService{

    @Autowired
    private CacheFeign cacheFeign;
    @Override
    public boolean strategy(StandardSubmit standardSubmit) {
        log.info("[策略模块-号段补全策略]开始执行！");
        //获取发送目标手机号
        String destMobile = standardSubmit.getDestMobile();
        if(CheckMobileOperator.isChinaMobilePhoneNum(destMobile)){
            //1.判断手机号是否为中国移动
            //网关ID
            standardSubmit.setGatewayID(OperatorConstants.CHINA_MOBILE);
            //运营商 0-未知 1-移动 2-联通 3-电信
            standardSubmit.setOperatorId(OperatorConstants.CHINA_MOBILE);
        }else if(CheckMobileOperator.isChinaUnicomPhoneNum(destMobile)){
            //2.判断手机号是否为中国联通
            //网关ID
            standardSubmit.setGatewayID(OperatorConstants.CHINA_UNICOM);
            //运营商 0-未知 1-移动 2-联通 3-电信
            standardSubmit.setOperatorId(OperatorConstants.CHINA_UNICOM);
        }else if(CheckMobileOperator.isChinaTelecomPhoneNum(destMobile)){
            //3.判断手机号是否为中国电信
            //网关ID
            standardSubmit.setGatewayID(OperatorConstants.CHINA_TELECOM);
            //运营商 0-未知 1-移动 2-联通 3-电信
            standardSubmit.setOperatorId(OperatorConstants.CHINA_TELECOM);
        }else{
            //4.判断是否为未知手机号
            //网关ID
            standardSubmit.setGatewayID(OperatorConstants.CHINA_UNKNOWN);
            //运营商 0-未知 1-移动 2-联通 3-电信
            standardSubmit.setOperatorId(OperatorConstants.CHINA_UNKNOWN);
        }

        //2.获取通道对象,网关id加通道id对应的通道对象
        Map<String,Object> channelMap = cacheFeign.hMGet(CacheConstants.CACHE_PREFIX_SMS_CHANNEL + standardSubmit.getGatewayID() + ":" + standardSubmit.getOperatorId());
        //3.获取路由对象，clientID+网关ID获取数据
        Map<String,Object> routerMap = cacheFeign.hMGet(CacheConstants.CACHE_PREFIX_ROUTER + standardSubmit.getClientID() + ":" + standardSubmit.getGatewayID());
        //4.获取目标手机号的前七位
        String mobileSub = destMobile.substring(0, 7);
        //5.获取目标手机号的省市归属地编号
        String provinceAndCityStr=cacheFeign.get(CacheConstants.CACHE_PREFIX_PHASE +mobileSub);
        if(StringUtils.isEmpty(provinceAndCityStr)){
            log.info("[策略模块-号段补全策略]，获取手机号归属地错误，无法获取到目标手机号的归属地，standardSubmit={}",standardSubmit);
            return false;
        }
        //6.切割省市编码字符串，获取到省市编码的数组
        String[] provinceAndCityStrArray = provinceAndCityStr.split(",");
        standardSubmit.setProvinceId(Integer.parseInt (provinceAndCityStrArray[0]));
        standardSubmit.setCityId(Integer.parseInt(provinceAndCityStrArray[1]));
        //7，组装下发源号码
        if(channelMap!=null&&routerMap!=null){
            String spnumber =String.valueOf(channelMap.get("spnumber"));
            String extendnumber =String.valueOf(routerMap.get("extendnumber"));
            Long price =Long.parseLong(String.valueOf(routerMap.get("price")));
            //下发源号码
            standardSubmit.setSrcNumber(spnumber+extendnumber);
            //当前短信费用
            standardSubmit.setFee(price);
        }
        log.info("[策略模块-号段补全策略]执行结束！");
        return true;
    }
}
